//JavaScript Framework 2.0 Code
try {
	Type.registerNamespace('com.yonyou.ncmm_pac_pda.DM_mainController');
	com.yonyou.ncmm_pac_pda.DM_mainController = function() {
		com.yonyou.ncmm_pac_pda.DM_mainController.initializeBase(this);
		this.initialize();
	}

	function com$yonyou$ncmm_pac_pda$DM_mainController$initialize() {}

	function com$yonyou$ncmm_pac_pda$DM_mainController$evaljs(js) {
		eval(js)
	}

	//返回按钮click事件
	function onPageBtnClick(sender, args) {
		$view.close();
	}

	//页面按钮切换事件
	function togglebtnchange(sender, args) {
		var value = $id("togglebuttongroup0").get("selectedvalue");
		//$alert("toggle:" + value);
		$id("flipperdefine0").set("viewindex", value);
		if(value == 0) {
			var selected = $cache.read("isAllChecked0"); //isAllChecked0:档案管理全选
			if(selected == null || selected == "" || selected == "false")
				selected = false;
			if(selected == "true")
				selected = true;

			$ctx.put("isAllChecked", selected);
			$ctx.dataBind();
			$id("button0").set("visible", true);
		} else {
			var selected = $cache.read("isAllChecked1"); //isAllChecked1:单据管理全选
			if(selected == null || selected == "" || selected == "false")
				selected = false;
			if(selected == "true")
				selected = true;

			$ctx.put("isAllChecked", selected);
			$ctx.dataBind();
			$id("button0").set("visible", false);
		}

	}

	//多页签左划事件
	function previous(sender, args) {
		var i = $id("flipperdefine0").get("viewindex");
		//		alert("next i=" + i);
		$id("togglebuttongroup0").set("selectedvalue", i);

		var selected = $cache.read("isAllChecked0"); //isAllChecked0:档案管理全选
		if(selected == null || selected == "" || selected == "false")
			selected = false;
		if(selected == "true")
			selected = true;

		$ctx.put("isAllChecked", selected);
		$ctx.dataBind();
		$id("button0").set("visible", true);

	}

	//多页签右划事件
	function next(sender, args) {
		var i = $id("flipperdefine0").get("viewindex");
		//		alert("next i=" + i);
		$id("togglebuttongroup0").set("selectedvalue", i);
		var selected = $cache.read("isAllChecked1");
		if(selected == null || selected == "false")
			selected = false;
		if(selected == "true")
			selected = true;

		$ctx.put("isAllChecked", selected);
		$ctx.dataBind();
		$id("button0").set("visible", false);

	}

	function pageload(sender, args) {
		NCMMDBUtil.initMappingTable();

		//加载单据管理页签显示数据
		//loadBillTab();

	}

	//加载基础档案管理页签显示全部数据
	function loadBaseDocTab() {
		$js.showLoadingBar({
			msg: "正在加载数据..."
		});
		var condition = {
			"location_unequal": "BODY",
			"datatype": "BD"
		};
		NCMMPubUtil.queryVOByConditions(AppmappingheadVO, condition, function(data) {
			var bdlistdata = eval(data);
			for(var i = 0; i < bdlistdata.length; i++) {
				if(bdlistdata[i]["downloadnum"] == "") {
					bdlistdata[i]["downloadnum"] = "0";
				}
				if(bdlistdata[i]["progressbar"] == "") {
					bdlistdata[i]["progressbar"] = "0";
				}
			}
			$ctx.put("bdlistdata", bdlistdata);
			$ctx.dataBind();
			$js.hideLoadingBar();
		});
	}

	function loadBillTab() {
		$cache.write("isAllChecked1", false);
		var data = [];
        //采购到货
		var json1 = {
            "appbillname": "采购订单到货",
            "apptablename": PoArriveVO.prototype.getTableName(),
            "scantablename": PoArriveScanVO.prototype.getTableName()
        };
		getBillDownInfo(json1,PoArriveVO,data);	
		//采购入库
		var json2 = {
			"appbillname": "采购到货入库",
			"apptablename": PurchaseInVO.prototype.getTableName(),
			"scantablename": PurchaseInScanedVO.prototype.getTableName()
		};
		getBillDownInfo(json2,PurchaseInVO,data);
		//采购订单入库
		var json3 = {
			"appbillname": "采购订单入库",
			"apptablename": PorderInVO.prototype.getTableName(),
			"scantablename": PorderInScanVO.prototype.getTableName()
		};	
		getBillDownInfo(json3,PorderInVO,data);        
		//调拨出库入库
		var json4 = {
            "appbillname": "调拨出库入库",
            "apptablename": TransInVO.prototype.getTableName(),
            "scantablename": TransInScanVO.prototype.getTableName()
        };
		getBillDownInfo(json4,TransInVO,data);
        //委外订单到货
		var json5 = {
            "appbillname": "委外订单到货",
            "apptablename": ScArriveVO.prototype.getTableName(),
            "scantablename": ScArriveScanVO.prototype.getTableName()
        };
		getBillDownInfo(json5,ScArriveVO,data);
		//委外到货入库
		var json6 = {
            "appbillname": "委外到货入库",
            "apptablename": SCArriveInVO.prototype.getTableName(),
            "scantablename": SCArriveInScanVO.prototype.getTableName()
        };
		getBillDownInfo(json6,SCArriveInVO,data);
		//委外订单入库
		var json7 = {
            "appbillname": "委外订单入库",
            "apptablename": SCorderInVO.prototype.getTableName(),
            "scantablename": SCorderInScanVO.prototype.getTableName()
        };
		getBillDownInfo(json7,SCorderInVO,data);
		//产成品入库		
		var json8 = {
			"appbillname": "产品应收入库",
			"apptablename": FINPRODINVO.prototype.getTableName(),
			"scantablename": FINPRODINSCANVO.prototype.getTableName()
		};
		getBillDownInfo(json8,FINPRODINVO,data);
		//其他应收入库
		var json9 = {
			"appbillname": "其他应收入库",
			"apptablename": GeneralInVO.prototype.getTableName(),
			"scantablename": GeneralInScanVO.prototype.getTableName()
		};	
		getBillDownInfo(json9,GeneralInVO,data);				
		//借出还回
		var json10 = {
			"appbillname": "借出还回",
			"apptablename": ReturnInVO.prototype.getTableName(),
			"scantablename": ReturnInScanVO.prototype.getTableName()
		};	
		getBillDownInfo(json10,ReturnInVO,data);
		//销售出库
		var json11 = {
			"appbillname": "销售发货出库",
			"apptablename": DeliveryVO.prototype.getTableName(),
			"scantablename": DeliveryScanVO.prototype.getTableName()
		};			
		getBillDownInfo(json11,DeliveryVO,data);	
		//销售订单出库
        var json12 = {
            "appbillname": "销售订单出库",
            "apptablename": SOVO.prototype.getTableName(),
            "scantablename": SOScanVO.prototype.getTableName()
        };
		getBillDownInfo(json12,SOVO,data);
        //销售应发出库
		 var json13 = {
            "appbillname": "销售应发出库",
            "apptablename": SaleOutVO.prototype.getTableName(),
            "scantablename": SaleOutScansVO.prototype.getTableName()
        };
		getBillDownInfo(json13,SaleOutVO,data);
		//材料应发出库
		var json14 = {
			"appbillname": "材料应发出库",
			"apptablename": MaterialOutVO.prototype.getTableName(),
			"scantablename": MaterialOutScansVO.prototype.getTableName()
		};
		getBillDownInfo(json14,MaterialOutVO,data);
		//调拨订单出库
		var json15 = {
            "appbillname": "调拨订单出库",
            "apptablename": TransVO.prototype.getTableName(),
            "scantablename": TransScanVO.prototype.getTableName()
        };
		getBillDownInfo(json15,TransVO,data);
		//调拨发货出库
		var json16 = {
			"appbillname": "调拨发货出库",
			"apptablename": TransDeliverOutVO.prototype.getTableName(),
			"scantablename": TransDeliverOutScanVO.prototype.getTableName()
		};
		getBillDownInfo(json16,TransDeliverOutVO,data);
		//调拨应发出库
		var json17 = {
			"appbillname": "调拨应发出库",
			"apptablename": TransOutVO.prototype.getTableName(),
			"scantablename": TransOutScanVO.prototype.getTableName()
		};
		getBillDownInfo(json17,TransOutVO,data);
		//出库申请单
		var json18 = {
            "appbillname": "出库申请出库",
            "apptablename": SapplyVO.prototype.getTableName(),
            "scantablename": SapplyScanVO.prototype.getTableName()
        };
		getBillDownInfo(json18,SapplyVO,data);
		//其他应发出库
		var json19 = {
			"appbillname": "其他应发出库",
			"apptablename": GeneralOutVO.prototype.getTableName(),
			"scantablename": GeneralOutScanVO.prototype.getTableName()
		};	
		getBillDownInfo(json19,GeneralOutVO,data);       
       //借入还回
       var json20 = {
			"appbillname": "借入还回",
			"apptablename": ReturnOutVO.prototype.getTableName(),
			"scantablename": ReturnOutScanVO.prototype.getTableName()
		};	
		getBillDownInfo(json20,ReturnOutVO,data);
		//库存盘点单
		var json21 = {
			"appbillname": "库存盘点单",
			"apptablename": INVCOUNTVO.prototype.getTableName(),
			"scantablename": INVCOUNTSCANVO.prototype.getTableName()
		};		
		getBillDownInfo(json21,INVCOUNTVO,data);
        //转库确认
         var json22 = {
            "appbillname": "转库确认",
            "apptablename": WhsTransVO.prototype.getTableName(),
            "scantablename": WhsTransScanVO.prototype.getTableName()
        };
		getBillDownInfo(json22,WhsTransVO,data);
		$ctx.put("billlistdata", data);
		$ctx.dataBind();
	}
	
	function getBillDownInfo(json,billvo,data){
		var isExist = NCMMPubUtil.isTableExist(billvo);
		if(isExist) {
			var resultvos = NCMMPubUtil.queryVONoCallback(billvo, null);
			if(resultvos == null || resultvos == "[]" || $stringToJSON(resultvos).length == 0) {
				json["downloadnum"] = 0;
			} else {
				json["downloadnum"] = $stringToJSON(resultvos).length;
			}
		} else {
			json["downloadnum"] = 0;
		}
		data.push(json);
	}

	function bdListLoad(sender, args) {
		//加载档案管理页签显示数据
		loadBaseDocTab();

	}

	//全选按钮单击事件（档案管理页签）
	function onCheckAll(sender, args) {
		var value = $id("togglebuttongroup0").get("selectedvalue");
		$ctx.dataCollect();
		var isChecked = $id(sender).get("checked");
		if(value == 0) {
			$cache.write("isAllChecked0", isChecked);
			var sourceData = $ctx.getJSONArray("bdlistdata");
			for(var i = 0; i < sourceData.length; i++) {
				sourceData[i]["ischecked"] = isChecked;
			}
			var json = {};
			json["bdlistdata"] = sourceData;
			json["isAllChecked"] = isChecked;
			json["billlistdata"] = $ctx.getJSONArray("billlistdata")
		} else {
			$cache.write("isAllChecked1", isChecked);
			var sourceData = $ctx.getJSONArray("billlistdata");
			for(var i = 0; i < sourceData.length; i++) {
				sourceData[i]["ischecked"] = isChecked;
			}
			var json = {};
			json["billlistdata"] = sourceData;
			json["isAllChecked"] = isChecked;
			json["bdlistdata"] = $ctx.getJSONArray("bdlistdata")
		}
		$ctx.push(json);

	}

	//列表行单击事件
	function dbListItemClick(sender, args) {
		var row = $id("listviewdefine0").get("row");
		row = $stringToJSON(row);
		//		if(row["downloadnum"] == null || row["downloadnum"] == "" || row["downloadnum"] == "0" ){
		//			$toast("档案【" + row["appbillname"] + "】没有本地数据。");
		//			return;
		//		}
		var viewid = "";
		switch(row["ncbillcode"]) {
			case "PDAMATERIAL":
				viewid = "com.yonyou.ncmm_pac_pda.DM_material_docDetail";
				break;
			case "PDABATCH":
				viewid = "com.yonyou.ncmm_pac_pda.DM_batchcode_docDetail";
				break;
			case "PDAMEASDOC":
				viewid = "com.yonyou.ncmm_pac_pda.DM_measdoc_docDetail";
				break;
			case "PDAWK":
				viewid = "com.yonyou.ncmm_pac_pda.DM_wk_docDetail";
				break;
			case "PDABARCODERULE":
				viewid = "com.yonyou.ncmm_pac_pda.DM_barcoderule_docDetail";
				break;
			case "PDABARCODEDICT":
				viewid = "com.yonyou.ncmm_pac_pda.DM_barcodedict_docDetail";
				break;
			case "PDARACK":
				viewid = "com.yonyou.ncmm_pac_pda.DM_rack_docDetail";
				break;
			case "PDASTORSTATE":
				viewid = "com.yonyou.ncmm_pac_pda.DM_storestate_docDetail";
				break;
			case "PDABILLTYPE":
				viewid = "com.yonyou.ncmm_pac_pda.DM_billtype_docDetail";
				break;
			default:
				viewid = "com.yonyou.ncmm_pac_pda.DM_comm_docDetail";
				break;
		};

		$view.open({
			"viewid": viewid, //目标页面（首字母大写）全名
			"isKeep": "true", //打开新页面的同时是否保留当前页面，true为保留，false为不保留
			"row": row,
			"callback": function() {
				reloadSingleRow(NCMMDBUtil.getDataVOByTablename(row["apptablename"]));
			}
		})
	}

	//行复选框选择事件
	function dbListitemCheckChanged(sender, args) {
		//检查是否需要改变全选复选框的状态
		checkAllbtnStatus();
	}

	//检查是否需要改变全选复选框的状态
	//全选勾选状态：某行取消勾选，则取消全选勾选
	//全选不勾选状态：所有行依次勾选，则最后全选勾选
	function checkAllbtnStatus() {
		var isCheckedAll = true;

		$ctx.dataCollect();
		var sourceData = $ctx.getJSONArray("bdlistdata");

		for(var i = 0; i < sourceData.length; i++) {
			if(sourceData[i]["ischecked"] == null || sourceData[i]["ischecked"] == false || sourceData[i]["ischecked"] == "false") {
				isCheckedAll = false;
				break;
			}
		}

		$ctx.put("isAllChecked", isCheckedAll); //更新全选复选框的界面显示
		$ctx.dataBind();

		var value = $id("togglebuttongroup0").get("selectedvalue");
		if(value == 0) {
			$cache.write("isAllchecked0", isCheckedAll);
		} else {
			$cache.write("isAllchecked1", isCheckedAll);
		}

	}

	//清除选中档案的本地记录
	function onClearLocalData(sender, args) {
		var value = $id("togglebuttongroup0").get("selectedvalue");
		if(value == 0) {
			var params = {
				"title": "档案管理",
				"message": "确定清除所选档案记录吗？",
				"okbuttontitle": "确定",
				"cancelbuttontitle": "取消",
				"style": "ok-cancel",
				"okaction": "doClear()",
				"cancelaction": "doCancel()"
			};

		} else {
			var params = {
				"title": "单据管理",
				"message": "删除单据会同时删除扫描记录？",
				"okbuttontitle": "确定",
				"cancelbuttontitle": "取消",
				"style": "ok-cancel",
				"okaction": "doClearBill()",
				"cancelaction": "doCancel()"
			};
		}
		$view.openDialog(params);
	}

	function doCancel() {
		$toast("清除操作已取消。");
	}

	//执行清除操作
	function doClear() {
		$app.showLoadingBar({
			msg: "准备清除档案..."
		});
		$ctx.dataCollect();
		var sourceData = $ctx.getJSONArray("bdlistdata");
		var needClear = false;
		for(var i = 0; i < sourceData.length; i++) {
			//过滤出需要下载的档案
			if(sourceData[i]["ischecked"] != null && (sourceData[i]["ischecked"] == true || sourceData[i]["ischecked"] == "true")) {
				var VOClass = NCMMDBUtil.getDataVOByTablename(sourceData[i]["apptablename"]);
				//具体数据处理	
				processDocDataClear(VOClass);
				needClear = true;
			}
		}
		//		$js.hideLoadingBar();
		if(needClear == false) {
			$app.hideLoadingBar();
			$toast("没有选中任何档案！");
		} else {
			$app.hideLoadingBar();
			$toast("选中档案清除！");
		}
	}

	//执行清除操作
	function doClearBill() {
		//		$js.showLoadingBar();
		$ctx.dataCollect();
		var sourceData = $ctx.getJSONArray("billlistdata");
		var needClear = false;
		for(var i = 0; i < sourceData.length; i++) {
			//过滤出需要下载的档案
			if(sourceData[i]["ischecked"] != null && (sourceData[i]["ischecked"] == true || sourceData[i]["ischecked"] == "true")) {
				var VOClass = getBillVOByTableName(sourceData[i]["apptablename"]);
				var ScanVOClass = getBillVOByTableName(sourceData[i]["scantablename"]);
				//具体数据处理	
				processBillDataClear(VOClass, ScanVOClass);
				needClear = true;
			}
		}
		//		$js.hideLoadingBar();
		if(needClear == false) {
			$toast("没有选中任何单据！");
		}
	}

	function getBillVOByTableName(tablename) {
		if(PurchaseInVO.prototype.getTableName() == tablename) {
			return PurchaseInVO;
		}
		if(FINPRODINVO.prototype.getTableName() == tablename) {
			return FINPRODINVO;
		}
		if(DeliveryVO.prototype.getTableName() == tablename) {
			return DeliveryVO;
		}
		if(SOVO.prototype.getTableName() == tablename) {
			return SOVO;
		}
		if(TransInVO.prototype.getTableName() == tablename) {
			return TransInVO;
		}
		if(WhsTransVO.prototype.getTableName() == tablename) {
			return WhsTransVO;
		}
		if(MaterialOutVO.prototype.getTableName() == tablename) {
			return MaterialOutVO;
		}
		if(INVCOUNTVO.prototype.getTableName() == tablename){
		    return INVCOUNTVO;
		}
		if(TransVO.prototype.getTableName() == tablename){
            return TransVO;
        }
        if(PoArriveVO.prototype.getTableName() == tablename){
            return PoArriveVO;
        }
          if(PoArriveScanVO.prototype.getTableName() == tablename){
            return PoArriveScanVO;
        }
		if(PurchaseInScanedVO.prototype.getTableName() == tablename) {
			return PurchaseInScanedVO;
		}
		if(FINPRODINSCANVO.prototype.getTableName() == tablename) {
			return FINPRODINSCANVO;
		}
		if(DeliveryScanVO.prototype.getTableName() == tablename) {
			return DeliveryScanVO;
		}
		if(SOScanVO.prototype.getTableName() == tablename) {
			return SOScanVO;
		}
		if(TransInScanVO.prototype.getTableName() == tablename) {
			return TransInScanVO;
		}
		if(WhsTransScanVO.prototype.getTableName() == tablename) {
			return WhsTransScanVO;
		}
		if(MaterialOutScansVO.prototype.getTableName() == tablename) {
			return MaterialOutScansVO;
		}
		if(INVCOUNTSCANVO.prototype.getTableName() == tablename){
		    return INVCOUNTSCANVO;
		}
		if(TransScanVO.prototype.getTableName() == tablename){
            return TransScanVO;
        }
        if(SaleOutVO.prototype.getTableName() == tablename){
            return SaleOutVO;
        }
        if(SaleOutScansVO.prototype.getTableName() == tablename){
            return SaleOutScansVO;
        }
		if(SapplyVO.prototype.getTableName() == tablename){
            return SapplyVO;
        }
        if(SapplyScanVO.prototype.getTableName() == tablename){
            return SapplyScanVO;            
        }
        if(PorderInVO.prototype.getTableName()==tablename){
		   return PorderInVO;
		}
		if(PorderInScanVO.prototype.getTableName()==tablename){
		   return PorderInScanVO;
		}
		if(GeneralInVO.prototype.getTableName()==tablename){
		   return GeneralInVO;
		}
		if(GeneralInScanVO.prototype.getTableName()==tablename){
		   return GeneralInScanVO;
		}
		if(GeneralOutVO.prototype.getTableName()==tablename){
		   return GeneralOutVO;
		}
		if(GeneralOutScanVO.prototype.getTableName()==tablename){
		   return GeneralOutScanVO;
		}
		if(TransOutVO.prototype.getTableName()==tablename){
		   return TransOutVO;
		}
		if(TransOutScanVO.prototype.getTableName()==tablename){
		   return TransOutScanVO;
		}
		if(ReturnInVO.prototype.getTableName()==tablename){
		   return ReturnInVO;
		}
		if(ReturnInScanVO.prototype.getTableName()==tablename){
		   return ReturnInScanVO;
		}
		if(ReturnOutVO.prototype.getTableName()==tablename){
		   return ReturnOutVO;
		}
		if(ReturnOutScanVO.prototype.getTableName()==tablename){
		   return ReturnOutScanVO;
		}
		if(TransDeliverOutVO.prototype.getTableName()==tablename){
		   return TransDeliverOutVO;
		}
		if(TransDeliverOutScanVO.prototype.getTableName()==tablename){
		   return TransDeliverOutScanVO;
		}
		
		if(ScArriveVO.prototype.getTableName()==tablename){
		   return ScArriveVO;
		}
		if(ScArriveScanVO.prototype.getTableName()==tablename){
		   return ScArriveScanVO;
		}
		if(SCArriveInVO.prototype.getTableName()==tablename){
		   return SCArriveInVO;
		}
		if(SCArriveInScanVO.prototype.getTableName()==tablename){
		   return SCArriveInScanVO;
		}
		if(SCorderInVO.prototype.getTableName()==tablename){
		   return SCorderInVO;
		}
		if(SCorderInScanVO.prototype.getTableName()==tablename){
		   return SCorderInScanVO;
		}
	}

	//具体单据数据处理	
	function processBillDataClear(VOClass, ScanVOClass) {
		//清空该业务单据以及其对应的扫描记录表中数据
		if(NCMMPubUtil.isTableExist(VOClass)) {
			NCMMPubUtil.deleteAll(VOClass);
			loadBillTab();
		}
		if(NCMMPubUtil.isTableExist(ScanVOClass)) {
			NCMMPubUtil.deleteAll(ScanVOClass);
		}
	}

	//具体数据处理	
	function processDocDataClear(VOClass) {
		//清空本地表记录
		if(VOClass.prototype.getTableName()==MaterialVO.prototype.getTableName()){
		  NCMMDBUtil.clearAllOldData(VOClass, MaterialConvertVO);
		}
		else{
		NCMMDBUtil.clearAllOldData(VOClass, null);
		}
		//更新本地app_mappinghead表中下载记录数和下载时间
		NCMMDBUtil.updateMappingTable(VOClass, 0, "", 0,false);
		
		//刷新页面显示数据
		reloadSingleRow(VOClass);
	}

	//下载按钮触发事件
	function onDownloadBaseData(sender, args) {
		window.uploadTotal = 0;
		window.uploadSum = 0;
		window.showLoading = true;
		$id("button0").set("disable", true);
		//测试连接
		NCMMDBUtil.testNCConnection(function(data) {
			if(data == false || data == "false") {
				$toast("连接服务器失败，无法下载档案");
				return;
			} else { //连接验证成功后的后续处理
				var sourceData = $ctx.getJSONObject();
				var needDownload = false;
				for(var i = 0; i < sourceData["bdlistdata"].length; i++) {
					var data_datail = (sourceData["bdlistdata"])[i];
					if(data_datail["ischecked"] != null && (data_datail["ischecked"] == true || data_datail["ischecked"] == "true")) {
						window.uploadSum = window.uploadSum + 1;
					}
				}
				//				$alert("uploadSum=" + (window.uploadSum).toString());

				for(var i = 0; i < sourceData["bdlistdata"].length; i++) {
					//过滤出需要下载的档案
					var data_datail = (sourceData["bdlistdata"])[i];
					if(data_datail["ischecked"] != null && (data_datail["ischecked"] == true || data_datail["ischecked"] == "true")) {
						var VOClass = NCMMDBUtil.getDataVOByTablename(data_datail["apptablename"]);
						//$alert(data_datail);
						var ncbillcode = data_datail["ncbillcode"];
						var appbillname = data_datail["appbillname"];
						var downtime=data_datail["downloadtime"];
						if(downtime!=null&&downtime!=""){
						   downtime=downtime.replace("年","-");
						   downtime=downtime.replace("月","-");
						   downtime=downtime.replace("日","");
						}
						//闭包
						(function(VOClass, ncbillcode, appbillname) {
							var vo = VOClass;
							var _ncbillcode = ncbillcode;
							var _appbillname = appbillname;
							var condition = {};

							switch(_ncbillcode) {
								case "PDASTORDOC":
									condition = getStordocCondition(condition);
									if(condition == null) {
										$toast("下载仓库档案前，请先下载库存组织。");
										window.uploadSum = window.uploadSum - 1;
									} else {
									    if(downtime!=null&&downtime!=""){
										     condition["creationtime"]=">='"+downtime+"'";
										}
										NCMMDBUtil.processDocDataFromNC2017(vo, _appbillname, condition, [], 0, null, true);
									}
									break;
								case "PDADEPT":
									condition = getOrgCondition(condition);
									if(condition == null) {
										$toast("下载部门档案前，请先下载库存组织。");
										window.uploadSum = window.uploadSum - 1;
									} else {
									    if(downtime!=null&&downtime!=""){
										     condition["creationtime"]=">='"+downtime+"'";
										}
										NCMMDBUtil.processDocDataFromNC2017(vo, _appbillname, condition, [], 0, null, true);
									}
									break;								
								case "PDAMATERIAL":
									//因为物料下载要根据默认库存组织增加过滤条件，所以需要特殊处理一下
									var stockorg = "";
									if($cache.read("defaultstockorg") && $cache.read("defaultstockorg") != "") {
										stockorg = ($stringToJSON($cache.read("defaultstockorg")))["pk_stockorg"];
									}
									//没有设置默认库存组织 -- 不允许下载，提示
									if(stockorg == "") {
										$toast("下载物料档案前，请先设置默认库存组织。");
										window.uploadSum = window.uploadSum - 1;
									} else {
										condition = {
											"pk_org": " IN('" + stockorg + "')",
										};
										if(downtime!=null&&downtime!=""){
										     condition["creationtime"]=">='"+downtime+"'";
										}
										var cvcondition = JSON.parse(JSON.stringify(condition));
										//$alert(condition);
										NCMMDBUtil.processMaterialData(vo, _appbillname, condition, [], 0, function(caller, data){
										//var mCount= NCMMPubUtil.queryVOCount(MaterialVO);
										//if(mCount>0){										 
										   NCMMDBUtil.processDocDataFromNC2017(MaterialConvertVO, "辅计量管理", cvcondition, [], 0, null, true);
									     // }
										}, true);
									}
									break;
								case "PDARACK":
									//货位需要按默认仓库下载
									var stockorg = "";
									if($cache.read("defaultstockorg") && $cache.read("defaultstockorg") != "") {
										stockorg = ($stringToJSON($cache.read("defaultstockorg")))["pk_stockorg"];
									}
									//没有设置默认库存组织 -- 不允许下载，提示
									if(stockorg == "") {
										$toast("下载货位档案前，请先设置默认库存组织。");
										window.uploadSum = window.uploadSum - 1;
									} else {
										condition = {
											"pk_org": " IN('" + stockorg + "')"
										};
										if(downtime!=null&&downtime!=""){
										     condition["creationtime"]=">='"+downtime+"'";
										}
										NCMMDBUtil.processDocDataFromNC2017(vo, _appbillname, condition, [], 0, null, true);
									}
									break;
								case "PDABARCODEDICT": 
									//									processDocDataFromNC(BarCodeDictVO, BarCodeDictBodyVO, _appbillname, null);
									if(downtime!=null&&downtime!=""){
										     condition["creationtime"]=">='"+downtime+"'";
										}									
									NCMMDBUtil.processDocDataFromNC3(BarCodeDictVO, BarCodeDictBodyVO, _appbillname, {}, null, true);
									break;
								case "PDABARCODERULE":
									//下载条码类型为物料码或单件码且为启用最新的条码规则
									var condition = {
										"islatest": "='Y'",
										"enablestate": "='2'",
										"pk_bartype": "in('1001Z01000000000DBXV','1001Z01000000000E28C')"
									};
									if(downtime!=null&&downtime!=""){
										     condition["creationtime"]=">='"+downtime+"'";
										}
									//									processDocDataFromNC(BarCodeRuleVO, BarCodeRuleBodyVO, _appbillname, condition);
									NCMMDBUtil.processDocDataFromNC3(BarCodeRuleVO, BarCodeRuleBodyVO, _appbillname, condition,null, true);
									break;
								case "PDASUPPLIER":
									var stockorg = "";
									if($cache.read("defaultstockorg") && $cache.read("defaultstockorg") != "") {
										stockorg = ($stringToJSON($cache.read("defaultstockorg")))["pk_stockorg"];
									}
									//没有设置默认库存组织 -- 不允许下载，提示
									if(stockorg == "") {
										$toast("下载供应商档案前，请先设置默认库存组织。");
										window.uploadSum = window.uploadSum - 1;
									} else {
									    condition = {
											"pk_org": " IN('" + stockorg + "')",
										};
									    if(downtime!=null&&downtime!=""){
										     condition["creationtime"]=">='"+downtime+"'";
										}
										NCMMDBUtil.processDocDataFromNC2017(vo, _appbillname, condition, [], 0, null, true);
									}
									break;			
								case "PDASTORSTATE":
								     if(downtime!=null&&downtime!=""){
										     condition["creationtime"]=">='"+downtime+"'";
										}
								    //var condition={
								     // "pk_group_code":"='"+$cache.read("groupcode")+"'"
								    //};
								    NCMMDBUtil.processDocDataFromNC2017(vo, _appbillname, condition, [], 0, null, true);
									break;
								case "PDABILLTYPE":
								    var condition = {
										"parentbilltype": "in('45','4D','46','4455','4A','4I','49','4H','4R','4K','4B','4J')"
									};
								    NCMMDBUtil.processDocDataFromNC2017(vo, _appbillname, condition, [], 0, null, true);
									break;
								default:
								    if(downtime!=null&&downtime!=""){
										     condition["creationtime"]=">='"+downtime+"'";
									}
									NCMMDBUtil.processDocDataFromNC2017(vo, _appbillname, condition, [], 0, null, true);
									break;
							}
							needDownload = true;

						})(VOClass, ncbillcode, appbillname);

					}
				}
				if(needDownload == false) {
					$toast("没有选中任何档案！");
				}
			}
		});
		
		$id("button0").set("disable", false);
	}

	//仓库档案数据的过滤条件
	//情况1-设置了默认库存组织：按默认库存组织主键做过滤条件
	//情况2-没有设置默认库存组织-没有下载库存组织：提示不能下载仓库档案
	//情况3-没有设置默认库存组织-已经下载了库存组织：已下载所有库存组织的主键作为过滤条件
	function getStordocCondition(condition) {
		var strSql = "IN (";
		//情况1
		if($cache.read("defaultstockorg") && $cache.read("defaultstockorg") != "") {
			var refdata = $stringToJSON($cache.read("defaultstockorg"));
			strSql = strSql + "'" + refdata["pk_stockorg"] + "')";
			condition["pk_stock"] = strSql;
			return condition;
		}
		var stockorgvos = NCMMPubUtil.queryVONoCallback(StockorgVO, null);
		stockorgvos = stockorgvos ? eval(stockorgvos) : null;
		if(stockorgvos == null || stockorgvos.length <= 0) { //情况2			
			return null;
		}
		//情况3
		for(var i = 0; i < stockorgvos.length; i++) {
			strSql = strSql + "'" + stockorgvos[i]["pk_stockorg"] + "',";
		}
		strSql = strSql.slice(0, strSql.length - 1);
		strSql = strSql + ")";

		condition["pk_stock"] = strSql;
		return condition;
	}
	
	//部门档案数据的过滤条件
	//情况1-设置了默认库存组织：按默认库存组织主键做过滤条件
	//情况2-没有设置默认库存组织-没有下载库存组织：提示不能下载仓库档案
	//情况3-没有设置默认库存组织-已经下载了库存组织：已下载所有库存组织的主键作为过滤条件
	function getOrgCondition(condition) {
		var strSql = "IN (";
		//情况1
		if($cache.read("defaultstockorg") && $cache.read("defaultstockorg") != "") {
			var refdata = $stringToJSON($cache.read("defaultstockorg"));
			strSql = strSql + "'" + refdata["pk_stockorg"] + "')";
			condition["pk_org"] = strSql;
			return condition;
		}
		var stockorgvos = NCMMPubUtil.queryVONoCallback(StockorgVO, null);
		stockorgvos = stockorgvos ? eval(stockorgvos) : null;
		if(stockorgvos == null || stockorgvos.length <= 0) { //情况2			
			return null;
		}
		//情况3
		for(var i = 0; i < stockorgvos.length; i++) {
			strSql = strSql + "'" + stockorgvos[i]["pk_stockorg"] + "',";
		}
		strSql = strSql.slice(0, strSql.length - 1);
		strSql = strSql + ")";

		condition["pk_org"] = strSql;
		return condition;
	}

	//具体数据处理	
	//1. 从NC端获取档案数据	NCMMDBUtil.loadNCBaseData
	//2.1 清空本地表记录			NCMMDBUtil.clearAllOldData
	//2.2 保存至本地数据库表	NCMMDBUtil.addNewDatas
	//3. 更新app_mappinghead表(下载记录数和下载时间)
	//4. 刷新页面显示数据
	//	function processDocDataFromNC(VOClass, VOClassBody, voname, condition) {
	//		if(window.showLoading) {
	//			$app.showLoadingBar({
	//				msg: "正在下载档案……"
	//			});
	//			window.showLoading = false;
	//		}
	//
	//		//1. 从NC端获取档案数据
	//		NCMMDBUtil.loadNCBaseData(VOClass, VOClassBody, NCMMHttpUtil.BDDOC, condition, function(data) {
	//			//									$alert("data="+$jsonToString(data));
	//			if(data == null) {
	//				NCMMDBUtil.hideLoading();
	//				enableButton();
	//				$toast("没有获取到服务端信息，档案【" + voname + "】不能更新。");
	//				return;
	//			}
	//			var props = Object.getOwnPropertyNames(data.billinfos) || [];
	//			if(props.length == 0) {
	//				if($isJSONObject(data)) {
	//					NCMMDBUtil.hideLoading();
	//					enableButton();
	//					$toast("没有下载到档案【" + voname + "】");
	//				} else {
	//					NCMMDBUtil.hideLoading();
	//					enableButton();
	//					$toast(data);
	//				}
	//				return;
	//			}
	//
	//			//如果没有请求到数据，则不更新本地表
	//			if(NCMMDBUtil.getBillCount(data) <= 0) {
	//				NCMMDBUtil.hideLoading();
	//				enableButton();
	//				$toast("没有获取到服务端信息，档案【" + voname + "】不能更新。");
	//				return;
	//			}
	//			//2.1 清空本地表记录
	//			NCMMDBUtil.clearAllOldData(VOClass, VOClassBody);
	//			//2.2 保存档案记录到本地数据库表
	//			//如果是主子结构，返回值是插入到本地主表中的记录数
	//			var vocount = NCMMDBUtil.addNewDatas(VOClass, VOClassBody, data);
	//			//3. 更新本地app_mappinghead表中下载记录数和下载时间
	//			NCMMDBUtil.updateMappingTable(VOClass, vocount, NCMMHttpUtil.getCurrentLocalTime(), "100");
	//			//4. 刷新页面显示数据
	//			reloadSingleRow(VOClass);
	//			NCMMDBUtil.hideLoading();
	//			enableButton();
	//			$toast("档案【" + voname + "】下载了【" + vocount + "】条数据。");
	//		});
	//	}

	//加载指定基础档案的显示数据
	function reloadSingleRow(VOClass) {
		var condition = {
			"datatype": "BD",
			"apptablename": VOClass.prototype.getTableName()
		};

		NCMMPubUtil.queryVOByConditions(AppmappingheadVO, condition, function(data) {
			var jsondata = {
				bdlistdata: eval(data)
			};
			var singledata = (jsondata.bdlistdata)[0];

			$ctx.dataCollect();
			var sourceData = $ctx.getJSONArray("bdlistdata");
			for(var i = 0; i < sourceData.length; i++) {
				if(sourceData[i]["pk_map"] == singledata["pk_map"]) {
					var rowCheck = sourceData[i]["ischecked"];
					if(singledata["downloadnum"] == "") {
						singledata["downloadnum"] = "0";
					}
					sourceData[i] = singledata;
					sourceData[i]["ischecked"] = rowCheck;
					break;
				}
			}

			$ctx.put("bdlistdata", sourceData);
			$ctx.dataBind();
		});
	}

	function loadmenu(sender, args) {
		NCMMPubMenu.openDropDownList("btn_menu");
	}
	//页面重新启动的时候调用
	function pageRestart(sender, args) {
		//bdListLoad(sender, args);
	}

	function billlistLoad(sender, args) {
		loadBillTab();
	}

	com.yonyou.ncmm_pac_pda.DM_mainController.prototype = {
		initialize: com$yonyou$ncmm_pac_pda$DM_mainController$initialize,
		evaljs: com$yonyou$ncmm_pac_pda$DM_mainController$evaljs
	};
	com.yonyou.ncmm_pac_pda.DM_mainController.registerClass('com.yonyou.ncmm_pac_pda.DM_mainController', UMP.UI.Mvc.Controller);
} catch(e) {
	$e(e);
}